package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.ArrayList;
import java.util.List;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

/*********************************************************
 * 文件名称：ExtResultMapWithBLOBsElementGenerator.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.xmlmapper.elements
 * 功能说明：让clob兼容mysql和oracle
 * 开发人员 @author：caoxy31978
 * 开发时间 @date：2023/11/23 15:11
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class ExtResultMapWithBLOBsElementGenerator extends ResultMapWithBLOBsElementGenerator{
    @Override
    public void addElements(XmlElement parentElement) {
        XmlElement answer = new XmlElement("resultMap"); //$NON-NLS-1$

        answer.addAttribute(new Attribute("id", //$NON-NLS-1$
                introspectedTable.getResultMapWithBLOBsId()));

        String returnType;
        if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
            returnType = introspectedTable.getRecordWithBLOBsType();
        } else {
            // table has BLOBs, but no BLOB class - BLOB fields must be
            // in the base class
            returnType = introspectedTable.getBaseRecordType();
        }

        answer.addAttribute(new Attribute("type", returnType)); //$NON-NLS-1$

        if (!introspectedTable.isConstructorBased()) {
            answer.addAttribute(new Attribute("extends", //$NON-NLS-1$
                    introspectedTable.getBaseResultMapId()));
        }

        context.getCommentGenerator().addComment(answer);

        if (introspectedTable.isConstructorBased()) {
            addResultMapConstructorElements(answer);
        } else {
            addResultMapElements(answer);
        }

        if (context.getPlugins().sqlMapResultMapWithBLOBsElementGenerated(answer, introspectedTable)) {
            parentElement.addElement(answer);
        }
    }

    private void addResultMapElements(XmlElement answer) {

        buildResultMapItems(ResultElementType.RESULT, introspectedTable.getBLOBColumns()).forEach(answer::addElement);
    }

    private void addResultMapConstructorElements(XmlElement answer) {
        answer.addElement(buildConstructorElement(true));
    }
    protected List<XmlElement> buildResultMapItems(ResultElementType elementType, List<IntrospectedColumn> columns) {
        List<XmlElement> answer = new ArrayList<>();
        for (IntrospectedColumn introspectedColumn : columns) {
            XmlElement resultElement = new XmlElement("result");

            resultElement.addAttribute(buildColumnAttribute(introspectedColumn));
            resultElement.addAttribute(new Attribute("property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
            resultElement.addAttribute(new Attribute("jdbcType",
                    introspectedColumn.getJdbcTypeName().equals("LONGVARCHAR")?"CLOB":introspectedColumn.getJdbcTypeName())); //$NON-NLS-1$

            if (stringHasValue(introspectedColumn.getTypeHandler())) {
                resultElement.addAttribute(
                        new Attribute("typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
            }
            answer.add(resultElement);
            // extAddEle(introspectedColumn, resultElement, answer);
        }

        return answer;
    }

    private static void extAddEle(IntrospectedColumn introspectedColumn, XmlElement resultElement,
                                  List<XmlElement> answer) {
        XmlElement ifMysqlElement = new XmlElement("if");
        ifMysqlElement.addAttribute(new Attribute("test", "${getDBType} == 'Mysql'"));
        XmlElement ifOracleElement = new XmlElement("if");
        ifOracleElement.addAttribute(new Attribute("test", "${getDBType} == 'Oracle'"));
        XmlElement xmlElement = new XmlElement(resultElement);
        List<Attribute> attributes = xmlElement.getAttributes();
        Attribute jdbcType = attributes.stream().filter(attribute -> attribute.getName().equals("jdbcType")).findFirst().get();
        attributes.remove(jdbcType);
        if (introspectedColumn.getJdbcTypeName().equalsIgnoreCase("CLOB")){
            ifOracleElement.addElement(resultElement);
            attributes.add(new Attribute("jdbcType", "LONGVARCHAR"));
            ifMysqlElement.addElement(xmlElement);
            answer.add(ifOracleElement);
            answer.add(ifMysqlElement);
        } else if (introspectedColumn.getJdbcTypeName().equalsIgnoreCase("LONGVARCHAR")) {
            ifOracleElement.addElement(xmlElement);
            attributes.add(new Attribute("jdbcType", "CLOB"));
            ifMysqlElement.addElement(resultElement);
            answer.add(ifOracleElement);
            answer.add(ifMysqlElement);
        }else{
            answer.add(resultElement);
        }

    }
}
